﻿<%@ WebHandler Language="C#" Class="RBContainerStepRecord" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using ReportBusiness;

/**
 * Description: 热表分厂派报工查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.4.20
 **/

public class RBContainerStepRecord : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string factoryid = "", workcenterid = "", teamid = "";
        JArray selectOrg = JArray.Parse(jObject["selectOrg"].ToString());
        if (selectOrg.Count == 3)
        {
            factoryid = selectOrg[0].ToString();
            workcenterid = selectOrg[1].ToString();
            teamid = selectOrg[2].ToString();
        }
        else if (selectOrg.Count == 2)
        {
            factoryid = selectOrg[0].ToString();
            workcenterid = selectOrg[1].ToString();
        }
        else
        {
            factoryid = selectOrg[0].ToString();
        }
        string sql = @"
SELECT f.factoryname,cci.mfgordername,cci.familylistnames,cci.productname,cci.productdescription,cci.containername,
       ctr.workcentername,ctr.workflowstepname,c.workflowstepname parentworkflowstepname,
       ctr.dispatchemployeename,ctr.dispatchdate,ctr.dispatchqty,
       ctr.teamname,
       ctr.reportemployeename,ctr.reportdate,ctr.reportqty,
       ctr.checkemployeename,ctr.checkdate,
       nvl(ctr.lossqty,0) lossqty,(ctr.reportqty - nvl(ctr.lossqty,0)) hegeQty
FROM containersteprecord ctr
LEFT JOIN containercurrentinfo cci ON ctr.containerid = cci.containerid
LEFT JOIN factory f ON f.factoryid = cci.originalfactoryid
LEFT JOIN 
(
  SELECT c.subcontainerid,c.workflowstepname,row_number() OVER(PARTITION BY c.subcontainerid,c.workflowstepname ORDER BY c.createdate) seq 
  FROM containersubcontainer c
  LEFT JOIN containercurrentinfo cci ON cci.containerid = c.subcontainerid
  WHERE cci.originalfactoryid = '{0}'
) c ON c.seq = 1 AND c.subcontainerid = ctr.containerid
WHERE cci.containerstatus = 1";
        // 分厂
        if (factoryid != "")
        {
            sql += string.Format(" AND ctr.factoryid = '{0}'", factoryid);
        }
        
        // 工区
        if (workcenterid != "")
        {
            sql += string.Format(" AND ctr.workcenterid = '{0}'", workcenterid);
        }

        // 班组
        if (teamid != "")
        {
            sql += string.Format(" AND ctr.teamid = '{0}'", teamid);
        }

        // 机型
        if (jObject["productfamilyname"] != null && jObject["productfamilyname"].ToString() != "")
        {
            sql += string.Format(" AND cci.familylistnames = '{0}'", jObject["productfamilyname"].ToString());
        }

        // 工序号
        if (jObject["specno"] != null && jObject["specno"].ToString() != "")
        {
            sql += string.Format(" AND ctr.specno like '%{0}%'", jObject["specno"].ToString());
        }

        // 零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        // 流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        // 派工人
        if (jObject["dispatcher"] != null && jObject["dispatcher"].ToString() != "")
        {
            sql += string.Format(" AND ctr.dispatchemployeeid = '{0}'", jObject["dispatcher"].ToString());
        }

        // 报工人
        if (jObject["worker"] != null && jObject["worker"].ToString() != "")
        {
            sql += string.Format(" AND ctr.reportemployeeid = '{0}'", jObject["worker"].ToString());
        }

        // 检验人
        if (jObject["checker"] != null && jObject["checker"].ToString() != "")
        {
            sql += string.Format(" AND ctr.checkemployeeid = '{0}'", jObject["checker"].ToString());
        }

        //子工艺批次
        if (jObject["workflowroute"] != null && jObject["workflowroute"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname LIKE '%{0}%'", jObject["workflowroute"].ToString());
        }

        // 派工日期
        if (jObject["dispatchdate"] != null && jObject["dispatchdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["dispatchdate"].ToString());
            sql += string.Format(" AND ctr.dispatchdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss') ", arr[0].ToString());
            sql += string.Format(" AND ctr.dispatchdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss') ", arr[1].ToString());
        }

        // 报工日期
        if (jObject["reportdate"] != null && jObject["reportdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["reportdate"].ToString());
            sql += string.Format(" AND ctr.reportdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss') ", arr[0].ToString());
            sql += string.Format(" AND ctr.reportdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss') ", arr[1].ToString());
        }

        // 检验日期
        if (jObject["checkdate"] != null && jObject["checkdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["checkdate"].ToString());
            sql += string.Format(" AND ctr.checkdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss') ", arr[0].ToString());
            sql += string.Format(" AND ctr.checkdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss') ", arr[1].ToString());
        }

        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        dt.Columns["factoryname"].Caption = "主制分厂";
        dt.Columns["mfgordername"].Caption = "订单号";
        dt.Columns["familylistnames"].Caption = "机型";
        dt.Columns["productname"].Caption = "零件图号";
        dt.Columns["productdescription"].Caption = "零件名称";
        dt.Columns["containername"].Caption = "流水卡号";
        dt.Columns["workcentername"].Caption = "责任工区";
        dt.Columns["workflowstepname"].Caption = "工序";
        dt.Columns["parentworkflowstepname"].Caption = "父工序";
        dt.Columns["dispatchemployeename"].Caption = "派工人";
        dt.Columns["dispatchdate"].Caption = "派工时间";
        dt.Columns["dispatchqty"].Caption = "派工数量";
        dt.Columns["teamname"].Caption = "班组";
        dt.Columns["reportemployeename"].Caption = "报工人";
        dt.Columns["reportdate"].Caption = "报工时间";
        dt.Columns["reportqty"].Caption = "报工数";
        dt.Columns["checkemployeename"].Caption = "检验员";
        dt.Columns["checkdate"].Caption = "检验日期";
        dt.Columns["lossqty"].Caption = "报废数";
        dt.Columns["hegeQty"].Caption = "合格数";
        ExportHelper.ExportExcel(dt, jObject["factoryname"].ToString() + "派报工查询报表");
    }
}